import Watcher from '../observer/watcher'
export function lifecycleMixin(Vue) {
    //加载vue的时候初始化_update函数
    console.log('始化_update函数')
    Vue.prototype._update = function (vnode, hydrating) {
        console.log('生成的vnode',vnode)
        const vm = this;
        const prevVnode = vm._vnode;
        const restoreActiveInstance = setActiveInstance(vm)
        vm._vnode = vnode
        if (!prevVnode) {
            //第一次渲染render
            console.log('调用渲染核心方法__patch__')
            vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */);
            console.log('重点：组件得到了渲染后的$el',vm.$el)
        } else {
            // updates
            vm.$el = vm.__patch__(prevVnode, vnode);
        }
        restoreActiveInstance()
        if (vm.$el) {
            vm.$el.__vue__ = vm
        }
        // if (vm.$vnode && vm.$parent && vm.$vnode === vm.$parent._vnode) {
        //     vm.$parent.$el = vm.$el
        // }
    }
}
export function initLifecycle (vm) {
    const options = vm.$options
    let parent = options.parent
    if (parent && !options.abstract) {
      parent.$children.push(vm)
    }
    vm.$parent = parent
    vm.$root = parent ? parent.$root : vm
    vm.$children = []
}
export let activeInstance = null
export function setActiveInstance(vm) {
    const prevActiveInstance = activeInstance
    activeInstance = vm
    return () => {
      activeInstance = prevActiveInstance
    }
}
export function mountComponent(vm, el, hydrating) {
    vm.$el = el
    let updateComponent;
    //定义组件更新
    //vm._render可以获取到虚拟dom，VNode 在renderMixin()cl
    //vm._update将虚拟dom转换成真实dom
    updateComponent = () => {
        //渲染真实dom
        vm._update(vm._render(), hydrating)
    }
    //TODO
    //调用始化_render函数
    console.log("调用始化_render函数")
    vm._update(vm._render(), hydrating)
    new Watcher(vm, updateComponent, ()=>{}, {
        before () {
        }
    }, true)
}